package com.zzl.study.nacos.properties;

import com.alibaba.cloud.commons.lang.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;

import java.util.Objects;

/**
 * Nacos注册中心yaml配置属性
 *
 * @author: zhangzl
 * @date: 2025/7/14 21:07
 * @version: 1.0
 */
@Slf4j
@Data
public class NacosServerConfiguration {

    /**
     * Nacos注册中心服务端地址
     */
    private String serverAddr;

    /**
     * Nacos服务端认证用户名
     */
    private String username;

    /**
     * Nacos服务端认证用密码
     */
    private String password;

    /**
     * Nacos服务端地址端点
     */
    private String endpoint;

    /**
     * Nacos注册中心命名空间
     */
    private String namespace;

    /**
     * 阿里云命名空间accessKey
     */
    private String accessKey;

    /**
     * 阿里云命名空间secretKey
     */
    private String secretKey;

    /**
     * 初始化Nacos注册中心配置属性
     *
     * @param registryName 注册中心名称
     * @param environment  环境变量
     */
    public void init(String registryName, Environment environment) {
        this.serverAddr = Objects.toString(this.serverAddr, "");
        if (this.serverAddr.endsWith("/")) {
            this.serverAddr = this.serverAddr.substring(0, this.serverAddr.length() - 1);
        }
        this.endpoint = Objects.toString(this.endpoint, "");
        this.namespace = Objects.toString(this.namespace, "");
        overrideFromEnv(registryName, environment);
    }

    /**
     * 从环境变量中覆盖属性
     *
     * @param registryName 注册中心名称
     * @param env          环境变量
     */
    private void overrideFromEnv(String registryName, Environment env) {
        if (StringUtils.isEmpty(this.getServerAddr())) {
            this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.multi-discovery.multiple." + registryName + ".server-addr:}"));
        }
        if (StringUtils.isEmpty(this.getNamespace())) {
            this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.multi-discovery.multiple." + registryName + ".namespace:}"));
        }
        if (StringUtils.isEmpty(this.getAccessKey())) {
            this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.multi-discovery.multiple." + registryName + ".access-key:}"));
        }
        if (StringUtils.isEmpty(this.getSecretKey())) {
            this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.multi-discovery.multiple." + registryName + ".secret-key:}"));
        }
        if (StringUtils.isEmpty(this.getEndpoint())) {
            this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.multi-discovery.multiple." + registryName + ".endpoint:}"));
        }
        if (StringUtils.isEmpty(this.getUsername())) {
            this.setUsername(env.resolvePlaceholders("${spring.cloud.nacos.multi-discovery.multiple." + registryName + ".username:}"));
        }
        if (StringUtils.isEmpty(this.getPassword())) {
            this.setPassword(env.resolvePlaceholders("${spring.cloud.nacos.multi-discovery.multiple." + registryName + ".password:}"));
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        NacosServerConfiguration that = (NacosServerConfiguration) o;
        return Objects.equals(serverAddr, that.serverAddr)
                && Objects.equals(username, that.username)
                && Objects.equals(password, that.password)
                && Objects.equals(endpoint, that.endpoint)
                && Objects.equals(namespace, that.namespace)
                && Objects.equals(accessKey, that.accessKey)
                && Objects.equals(secretKey, that.secretKey);
    }

    @Override
    public int hashCode() {
        return Objects.hash(serverAddr, username, password, endpoint, namespace, accessKey, secretKey);
    }

}
